﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using QLHD.BLL;
using QLHD.DAL;

namespace QLHD
{
    public partial class frmCT_BocLua : Form
    {
        string maauto;
        public frmCT_BocLua() { }
        public frmCT_BocLua(frmMain form)
        {
            InitializeComponent();
            MdiParent = form;
        }

        private void frmCT_BocLua_Load(object sender, EventArgs e)
        {
            LoadCombobox();
            dgvBocLua.DataSource = BocLua_DB.SelectAll();
            cbbMaLo.Text = "--Chọn mã lô--";
            cbbMaTP.Text = "--Chọn mã loại--";
            cbbMaNX.Text = "--Chọn mã N/X--";
            DisplayData(0);
            ReadOnlyTextbox();
        }
        private void DisplayData(int index)
        {
            try
            {
                txtMaCT_Boclua.Text = dgvBocLua.Rows[index].Cells[0].Value.ToString();
                cbbMaLo.Text = dgvBocLua.Rows[index].Cells[1].Value.ToString();
                cbbMaNX.Text = dgvBocLua.Rows[index].Cells[2].Value.ToString();
                cbbMaTP.Text = dgvBocLua.Rows[index].Cells[3].Value.ToString();
                txtDienGiai.Text = dgvBocLua.Rows[index].Cells[4].Value.ToString();
                txtKL_hangvo.Text = dgvBocLua.Rows[index].Cells[5].Value.ToString();
                txtKL_Hangnguyen.Text = dgvBocLua.Rows[index].Cells[6].Value.ToString();
                txtKL_Hangbe.Text = dgvBocLua.Rows[index].Cells[7].Value.ToString();
                txtXuatPL.Text = dgvBocLua.Rows[index].Cells[8].Value.ToString();
                dtpNgayThang.Text = dgvBocLua.Rows[index].Cells[9].Value.ToString();
                txtKL_ThuHoi.Text = (double.Parse(txtKL_Hangnguyen.Text) + double.Parse(txtKL_Hangbe.Text)).ToString();
            }
            catch
            {
                return;
            }

        }
        void LoadCombobox()
        {
            cbbMaLo.DataSource = Lo_DB.SelectLo();
            cbbMaLo.DisplayMember = "diengiai";
            cbbMaLo.ValueMember = "malo";

            cbbMaTP.DataSource = MaTP_DB.SelectMaTP_PhanLoai();
            cbbMaTP.DisplayMember = "matp";
            cbbMaTP.ValueMember = "matp";

            cbbMaNX.DataSource = MaNX_DB.SelectAllMaNX();
            cbbMaNX.DisplayMember = "manx";
            cbbMaNX.ValueMember = "manx";

        }

        private void btnThem_Click(object sender, EventArgs e)
        {
            if (btnThem.Text == "Tạo mới")
            {
                btnThem.Text = "Lưu";
                dtpNgayThang.Text = DateTime.Now.ToString();                
                AutoMaXuatNLTho();
                txtMaCT_Boclua.Text = maauto;
                cbbMaLo.Text = "--Chọn mã lô--";
                cbbMaTP.Text = "--Chọn mã loại--";
                cbbMaNX.Text = "--Chọn mã N/X--";
                NotReadOnlyTextbox();
                ClearTextbox();

            }
            else
            {
                try
                {
                    if (txtKL_Hangbe.Text == "")
                    {
                        txtKL_Hangbe.Text = "0";
                    }
                    if (txtKL_hangvo.Text =="")
                    {
                        txtKL_hangvo.Text = "0";
                    }
                    if (txtKL_Hangnguyen.Text == "")
                    {
                        txtKL_Hangnguyen.Text = "0";
                    }
                    if (txtXuatPL.Text == "")
                    {
                        txtXuatPL.Text = "0";
                    }
                    InsertTN_TN();
                    frmCT_BocLua_Load(sender, e);
                }
                catch (SqlException ex)
                {
                    MessageBox.Show("Xảy ra lỗi" + "\n" + ex.Message, "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    txtDienGiai.Focus();
                }
            }         
        }
        private void InsertTN_TN()
        {
            BocLua item = new BocLua();
            item.MaCT_Boclua = txtMaCT_Boclua.Text;
            item.Malo = cbbMaLo.SelectedValue.ToString();
            item.MaNX = cbbMaNX.Text;
            item.DienGiai = txtDienGiai.Text;
            item.MaTP = cbbMaTP.SelectedValue.ToString();
            item.Ngaythang = dtpNgayThang.Text;
            try
            {
                item.KL_hangvo = double.Parse(txtKL_hangvo.Text);
                item.KL_hangnguyen = double.Parse(txtKL_Hangnguyen.Text);
                item.KL_hangbe = double.Parse(txtKL_Hangbe.Text);
                item.XuatPL = double.Parse(txtXuatPL.Text);
            }
            catch (Exception)
            {
                item.KL_hangvo = 0;
                item.KL_hangnguyen = 0;
                item.KL_hangbe = 0;
                item.XuatPL = 0;
            }
            BocLua_DB.Insert(item);
            btnThem.Text = "Tạo mới";
            ReadOnlyTextbox();
        }
        private void ClearTextbox()
        {
            txtDienGiai.Clear();
            txtKL_hangvo.Clear();
            txtKL_Hangnguyen.Clear();
            txtKL_Hangbe.Clear();
        }

        private void NotReadOnlyTextbox()
        {
            txtDienGiai.ReadOnly = false;
            txtKL_hangvo.ReadOnly = false;
            txtKL_Hangnguyen.ReadOnly = false;
            txtKL_Hangbe.ReadOnly = false;
        }
        private void ReadOnlyTextbox()
        {
            txtDienGiai.ReadOnly = true;
            txtKL_hangvo.ReadOnly = true;
            txtKL_Hangnguyen.ReadOnly = true;
            txtKL_Hangbe.ReadOnly = true;
        }
        private void AutoMaXuatNLTho()
        {
            maauto = "";
            SqlConnection con = Connection.GetConnection();
            con.Open();
            string select = "select top 1 MaCT_BocLua from tblCT_BocLua where NgayThang = '" + dtpNgayThang.Text + "' order by MaCT_BocLua desc";
            DataTable dt = new DataTable();
            SqlCommand command = new SqlCommand(select, con);
            SqlDataAdapter da = new SqlDataAdapter(command);
            da.Fill(dt);

            if (dt.Rows.Count == 0)
            {
                maauto = dtpNgayThang.Text.Replace("/", "") + "1";
            }
            else
            {
                maauto = (Convert.ToInt32(dt.Rows[0][0]) + 1).ToString();
            }
        }

        private void btnSua_Click(object sender, EventArgs e)
        {
            try
            {
                if (btnSua.Text == "Cập nhật")
                {
                    btnSua.Text = "Lưu";
                    NotReadOnlyTextbox();

                }
                else
                {
                    BocLua item = new BocLua();
                    item.MaCT_Boclua = txtMaCT_Boclua.Text;
                    item.Malo = cbbMaLo.SelectedValue.ToString();
                    item.MaNX = cbbMaNX.Text;
                    item.DienGiai = txtDienGiai.Text;
                    item.MaTP = cbbMaTP.SelectedValue.ToString();
                    item.Ngaythang = dtpNgayThang.Text;
                    try
                    {
                        item.KL_hangvo = double.Parse(txtKL_hangvo.Text);
                        item.KL_hangnguyen = double.Parse(txtKL_Hangnguyen.Text);
                        item.KL_hangbe = double.Parse(txtKL_Hangbe.Text);
                        item.XuatPL = double.Parse(txtXuatPL.Text);
                    }
                    catch (Exception)
                    {
                        item.KL_hangvo = 0;
                        item.KL_hangnguyen = 0;
                        item.KL_hangbe =0;
                        item.XuatPL = 0;
                    }

                    BocLua_DB.Update(txtMaCT_Boclua.Text, item);
                    btnSua.Text = "Cập nhật";
                    frmCT_BocLua_Load(sender, e);
                    ReadOnlyTextbox();
                }
            }
            catch (SqlException ex)
            {
                MessageBox.Show("Xảy ra lỗi" + "\n" + ex.Message, "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
                txtKL_hangvo.Focus();
            }
        }

        private void btnHuy_Click(object sender, EventArgs e)
        {
            frmCT_BocLua_Load(sender, e);
            btnThem.Text = "Tạo mới";
            btnSua.Text = "Cập nhật";
        }

        private void btnDong_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            try
            {
                BocLua_DB.Delete(txtMaCT_Boclua.Text);
                frmCT_BocLua_Load(sender, e);
            }
            catch (SqlException ex)
            {
                MessageBox.Show("Xảy ra lỗi" + "\n" + ex.Message, "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
                txtKL_hangvo.Focus();
            }
        }

        private void dtpNgayThang_Leave(object sender, EventArgs e)
        {
            AutoMaXuatNLTho();
            txtMaCT_Boclua.Text = maauto;
        }

        private void dgvBocLua_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            DisplayData(e.RowIndex);
        }
    }
}
